package com.sit.controller;

import com.sit.pojo.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.sit.service.*;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class adminController {
    @Autowired
    private adminService adminService;
    @Autowired
    private ReadersService readersService;

    @RequestMapping("/adminloginHtml")
    public String adminloginHtml(){
        return "admin/adminLogin";
    }

    @RequestMapping("/index")
    public String index(Model model){
        List<books> books = adminService.findAllBook();
        model.addAttribute("booksList",books);
        return "admin/index";
    }

    @RequestMapping("/bookTypeHtml")
    public String bookTypeHtml(Model model){
        List<books_type> bookTypes = adminService.findAllBookType();
        model.addAttribute("bookTypes",bookTypes);
        return "admin/bookTypeManagement";
    }

    @RequestMapping("/addBookTypeHtml")
    public String addBookTypeHtml(){
        return "admin/insertBookType";
    }

    @RequestMapping("/addBookHtml")
    public String addBookHtml(){
        return "admin/insertBook";
    }

    @RequestMapping("/readerManageHtml")
    public String readerManageHtml(Model model){
        List<Readers> readers = adminService.findAllReader();
        model.addAttribute("readersList",readers);
        return "admin/readerManagement";
    }

    @RequestMapping("cardManageHtml")
    public String cardManageHtml(Model model){
        List<js_card> jsCardList = adminService.findAllReaderCard();
        model.addAttribute("cardList",jsCardList);
        return "admin/cardManagement";
    }

    @RequestMapping("/borrowManageHtml")
    public String borrowManageHtml(Model model){
        List<borrow> borrowList = adminService.NowBorrow();
        model.addAttribute("borrowList",borrowList);
        return "admin/borrowManagement";
    }

    @RequestMapping("/hisborrowHtml")
    public String hisborrowHtml(Model model){
        List<hisborrow> hisborrowList = adminService.findAllBorrow();
        model.addAttribute("hisborrowList",hisborrowList);
        return "admin/hisborrowManagement";
    }

    @RequestMapping("/punishManageHtml")
    public String punishManageHtml(Model model){
        List<punish> punishList = adminService.findAllPunish();
        model.addAttribute("punishList",punishList);
        return "admin/punishManagement";
    }

    @RequestMapping("updatepassHtml")
    public String updatepassHtml(){
        return "admin/updatePass";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestParam("name") String name, @RequestParam("password") String password, @RequestParam("captcha") String captcha, HttpSession session, HttpServletRequest request) throws Exception {
        administrator adminInfo = null;
        String Token = CaptchaController.verifyCaptcha(request, captcha);
        adminInfo = adminService.login(name);
        if(Token.equals("success")){
            if(adminInfo == null)
                return "1";
            if (password.equals(adminInfo.getAdminPass())) {
                session.setAttribute("adminId", adminInfo.getAdminId());
                return "2";
            }else {
                return "3";
            }
        }
        else return "4";
    }

    @RequestMapping("/goOut")
    public String goOut(HttpSession session){
        session.removeAttribute("adminId");
        return "redirect:/admin/adminloginHtml";
    }

    @RequestMapping("/getAdminId")
    @ResponseBody
    public String getAdminId(HttpSession session){
        return (String) session.getAttribute("adminId");
    }

    @RequestMapping("/updatepass")
    @ResponseBody
    public String updatepass(@RequestParam("oldPass")String oldPass,@RequestParam("newPass")String newPass,@RequestParam("againNewPass")String againNewPass,@RequestParam("adminId")String adminId){
        int result = 0;
        administrator admin = adminService.findAdminById(adminId);
        if (admin != null){
            if(admin.getAdminPass().equals(oldPass)){
                if(newPass.equals(againNewPass)){
                    admin.setAdminPass(newPass);
                    result = adminService.updatePwd(admin);
                    if (result != 0){
                        return "1";
                    }else return "0";
                }else return "2";
            }else return "3";
        }else return "0";
    }

    @RequestMapping("/deleteReader")
    @ResponseBody
    public String deleteReader(String readId){
        int result = 0;
        int result1 = 0;
        js_card card = readersService.findReaderCard(readId);
        String res = "false";
        if(card == null){
            result = adminService.deleteReader(readId);
            if(result!= 0) {
                res = "success";
            }
        }else if(card.getJsId() != null){
            result1 = adminService.deleteReaderCard(card.getReadId());
            result = adminService.deleteReader(readId);
            if(result!= 0 && result1!=0) {
                res = "success";
            }
        }
        return res;
    }

    @RequestMapping("/deleteReaderCard")
    @ResponseBody
    public String deleteReaderCard(String readId){
        int result = 0;
        String res = "false";
        result = adminService.deleteReaderCard(readId);
        if(result != 0)
            res = "success";
        return res;
    }

    @RequestMapping("/findReaderById")
    @ResponseBody
    public Readers findReaderById(String readId){
        Readers reader = null;
        reader = adminService.findReaderById(readId);
        return reader;
    }

    @RequestMapping("/updateReader")
    @ResponseBody
    public String updateReader(Readers reader){
        int result = 0;
        String res = "false";
        result = adminService.updateReader(reader);
        if(result!= 0)
            res = "success";
        return res;
    }

    @RequestMapping("/findReaderByName")
    @ResponseBody
    public String findReaderByName(String name){
        List<Readers> reader = null;
        reader = adminService.findReaderByName(name);
        return reader.toString();
    }

    @RequestMapping("/findBookByInput")
    @ResponseBody
    public List<books> findBookByInput(books book){
        return adminService.findBookByInput(book);
    }

    @RequestMapping("/findBookTypeByInput")
    @ResponseBody
    public List<books_type> findBookTypeByInput(books_type bookType){
        return adminService.findBookTypeByInput(bookType);
    }

    @RequestMapping("/findReaderByInput")
    @ResponseBody
    public List<Readers> findReaderByInput(String readId,String readName,String readSex,Integer readTel,String readMail){
        Readers reader = new Readers();
        reader.setReadId(readId);
        reader.setReadName(readName);
        reader.setReadSex(readSex);
        if(readTel != null) reader.setReadTel(readTel);
        reader.setReadMail(readMail);
        return adminService.findReaderByInput(reader);
    }

    @RequestMapping("/insertBook")
    public String insertBook(books book){
        int result = 0;
        result = adminService.insertBook(book);
        if(result != 0)
            return "redirect:/admin/index";
        return "500";
    }

    @RequestMapping("/deletebook")
    public String deleteBook(String bookId){
        int result = 0;
        result = adminService.deleteBook(bookId);
        if(result != 0)
            return "redirect:/admin/index";
        else
            return "500";
    }

    @RequestMapping("/updatebook")
    public String updateBook(books book){
        int result = 0;
        result = adminService.updateBook(book);
        if(result != 0)
            return "redirect:/admin/index";
        else
            return "500";
    }

    @RequestMapping("/findbook")
    @ResponseBody
    public books findBookById(@RequestParam("bookId")String bookId){
        books book = null;
        book = adminService.findBookById(bookId);
        return book;
    }

    @RequestMapping("/findbookByName")
    @ResponseBody
    public String findBookByName(String name){
        List<books> book = null;
        book = adminService.findBookByName(name);
        return book.toString();
    }

    @RequestMapping("addBookType")
    public String addBookType(books_type bookType){
        int result = 0;
        result = adminService.insertBookType(bookType);
        if(result != 0)
            return "redirect:/admin/bookTypeHtml";
        else
            return "500";
    }

    @RequestMapping("/deleteBookType")
    @ResponseBody
    public String deleteBookType(String typeId){
        int result = 0;
        String res = "false";
        result = adminService.deleteBookType(typeId);
        if(result != 0)
            res = "success";
        return res;
    }

    @RequestMapping("/updateBookType")
    public String updateBookType(books_type book){
        int result = 0;
        result = adminService.updateBookType(book);
        if(result != 0)
            return "redirect:/admin/bookTypeHtml";
        else
            return "500";
    }

    @RequestMapping("/findBookTypeById")
    @ResponseBody
    public books_type findBookTypeById(@RequestParam("typeId")String typeId){
        books_type bookType = null;
        bookType = adminService.findBookTypeById(typeId);
        return bookType;
    }

    @RequestMapping("/deleteBorrow")
    public String deleteBorrow(hisborrow borrow){
        int result = 0;
        result = adminService.deleteBorrow(borrow);
        if(result != 0)
            return "redirect:/admin/hisborrowHtml";
        else
            return "500";
    }

    @RequestMapping("/getReadersList")
    @ResponseBody
    public List<Readers> getReadersList(){
        return adminService.findAllReader();
    }

    @RequestMapping("/getReaderCardList")
    @ResponseBody
    public List<js_card> getReaderCardList(){
        return adminService.findAllReaderCard();
    }

    @RequestMapping("/getBooksList")
    @ResponseBody
    public List<books> getBookList(){
        return adminService.findAllBook();
    }

    @RequestMapping("/getBookTypeList")
    @ResponseBody
    public List<books_type> getBookTypeList(){
        return adminService.findAllBookType();
    }
}
